var ws = {
	connect: function() {
		var conn = new WebSocket("ws://localhost:7070/gocase");
		conn.onmessage = function(e) {
			console.log("received:", e.data);
			var msg = JSON.parse(e.data);
			args = [];
			for(var k in msg) if(k != "T") args.push(msg[k]);
			handle[msg["T"]].apply(this, args);
		};
		ws.conn = conn;
	},
	send: function send(type, args) {
		var data = type + JSON.stringify(args);
		console.log("sending:", data);
		ws.conn.send(data);
	}
};

var handle = {
	join_result: function(error) {
		if(error != "") {
			$("#join").removeAttr("disabled");
		} else {
		}
	}
};

function join() {
    $("#join").attr("disabled", true);
    ws.send("join", {"Nickname": $("#nickname").val()});
}

function show(pageName) {
	$(".page").fadeOut();
	$("#" + pageName).fadeIn();
}

$(document).ready(function() {
	ws.connect();
	
    $("#nickname").keypress(function(e) {
        if(e.keyCode == 13) join();
    });
    $("#join").click(join);

    $("#login_page").fadeIn();
    $("#nickname").focus();
});
